<?php
class ShoppingController extends Zend_Controller_Action{
	
	function init(){
		parent::init();
		$layoutPath = APPLICATION_PATH. '/templates/front/default';
		$option = array('layout'=>'index', 'layoutPath'=>$layoutPath);
		Zend_Layout::startMvc($option);
	}

    public function addAction(){
        $yourCart = new Zend_Session_Namespace('cart');
        $id = $this->_getParam('ma',0);
        $soluong = $_POST['soluong'];
        if(isset($yourCart->cart[$id])){
            $yourCart->cart[$id]['so_luong']+= $soluong;
            $yourCart->cart[$id]['thanh_tien'] = $yourCart->cart[$id]['so_luong']*$yourCart->cart[$id]['don_gia'];
        }
        else{
            $listproduct = new Front_Model_Product();
            $product = $listproduct->view($id);
            foreach($product as $vl){
                $dongia = $vl[don_gia];
                $ten = $vl[ten];
            }
            $yourCart->cart[$id] = array('ten'=>$ten, 'don_gia'=>$dongia, 'so_luong'=>$soluong, 'thanh_tien'=>$soluong*$dongia);
        }
        $url = '/Shopping/viewcart';
        $this->_redirect($url);
    }
    public function viewcartAction(){
        $yourCart = new Zend_Session_Namespace('cart');
		if($this->_request->isPost()){
			$itemProduct = $this->_arrParam['itemProduct'];
			if(count($itemProduct)>0)
				foreach($itemProduct as $key => $val){
					if($val == 0 ){
						unset($itemProduct[$key]);
				}
			}
			$yourCart->cart = $itemProduct;
		}
        $this->view->cart = $yourCart->cart;
    }
    
    public function deleteAction(){
    	$yourCart = new Zend_Session_Namespace('cart');
        $ma = $this->_getParam('ma',0);
        unset($yourCart->cart[$ma]);
        $url = '/Shopping/viewcart';
        $this->_redirect($url);
    }
    
    public function updateAction(){

    }
}
